package com.songyang.yougnsql.core.data;

import com.songyang.yougnsql.common.YoungSQLException;
import com.songyang.yougnsql.core.data.dataItem.DataItem;
import com.songyang.yougnsql.core.data.logger.Logger;
import com.songyang.yougnsql.core.data.page.PageOne;
import com.songyang.yougnsql.core.data.pagecache.PageCache;
import com.songyang.yougnsql.core.data.recover.Recover;
import com.songyang.yougnsql.core.transaction.TransactionManager;

public interface DataManager {
    public static DataManager create(String path, long mem, TransactionManager tm) throws YoungSQLException {
        PageCache pc = PageCache.open(path, mem);
        Logger lg = Logger.open(path);
        DataManagerImpl dm = new DataManagerImpl(pc, lg, tm);
        dm.initPageOne();
        return dm;
    }

    public static DataManager open(String path, long mem, TransactionManager tm) throws YoungSQLException {
        PageCache pc = PageCache.open(path, mem);
        Logger lg = Logger.open(path);
        DataManagerImpl dm = new DataManagerImpl(pc, lg, tm);
        if(!dm.loadCheckPageOne()) {
            Recover.recover(tm, lg, pc);
        }
        dm.fillPageIndex();
        PageOne.setVcOpen(dm.pageOne);
        dm.pageCache.flushPage(dm.pageOne);
        return dm;
    }

    DataItem read(long uid) throws Exception;

    long insert(long xid, byte[] data) throws Exception;

    void close();
}
